using Engine;

namespace RuthlessConquest {
    public class CapturePlanetParticleSystem : ParticleSystem<CapturePlanetParticleSystem.Particle> {
        public class Particle : global::RuthlessConquest.Particle {
            public Vector2 Velocity;

            public float TimeToLive;

            public Color BaseColor;
        }

        public Random Random = new();

        public Vector2 Position;

        public float Radius;

        public Color Color;

        public float Duration;

        public float ToGenerate;

        public CapturePlanetParticleSystem(Vector2 position, float radius, Color color) : base(120, 1) {
            Position = position;
            Radius = radius;
            Color = color;
            Texture = Textures.Explosion;
            TextureSlotsCount = 1;
        }

        public override bool Run(float dt) {
            dt = MathUtils.Clamp(dt, 0f, 0.1f);
            float num = MathUtils.Pow(0.09f, dt);
            Duration += dt;
            ToGenerate += 400f * dt;
            bool flag = false;
            for (int i = 0; i < Particles.Length; i++) {
                Particle particle = Particles[i];
                if (particle.IsActive) {
                    flag = true;
                    particle.TimeToLive -= dt;
                    if (particle.TimeToLive > 0f) {
                        particle.Position += particle.Velocity * dt;
                        particle.Velocity *= num;
                        particle.Color = particle.BaseColor * MathUtils.Saturate(0.5f * particle.TimeToLive);
                    }
                    else {
                        particle.IsActive = false;
                    }
                }
                else if (Duration < 0.7f) {
                    flag = true;
                    if (ToGenerate >= 1f) {
                        ToGenerate -= 1f;
                        particle.IsActive = true;
                        float num2 = Random.Float(0.1f, 1f);
                        Vector2 vector = Random.Vector2(1f);
                        particle.Position = Position + Radius * vector;
                        particle.BaseColor = Color;
                        particle.Size = new Vector2(MathUtils.Lerp(36f, 24f, num2));
                        particle.TimeToLive = MathUtils.Lerp(3f, 1.5f, num2) + Random.Float(-0.5f, 1f);
                        particle.Velocity = 1300f * num2 * vector + 0f * Random.Vector2(1f);
                        particle.UseAdditiveBlending = true;
                    }
                }
            }
            return !flag;
        }
    }
}